package com.cqut.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.dto.ComProcessDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.ComProcess;
import com.cqut.service.IComProcessService;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/comProcess")
public class ComProcessController {
    @Resource(name="comProcessService")
    private IComProcessService comProcessService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> comProcessList = comProcessService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        long count = comProcessService.count();

        result.put("rows", comProcessList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    @RequestMapping(value = "/stageList", method = RequestMethod.GET)
    public ResponseEntity<String> stageList(
    		@RequestParam("competitionId")
            String competitionId,
            @RequestParam("comProcessName")
            String comProcessName,
            @Validated
            LimitShowDTO limitShowDTO, BindingResult errors) {
    	if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
    	System.out.println(competitionId + " " + comProcessName);
    	Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> comProcessList = comProcessService.findStage( competitionId, comProcessName, limitShowDTO);
        long count = comProcessService.getCountByComProcessName(competitionId, comProcessName);
        result.put("rows", comProcessList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "/{comProcessId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("comProcessId") String comProcessId) {
        Map<String, Object> comProcessModel = comProcessService.findMap(comProcessId);

        return ResponseUtil.getResEntityForGetAndJson(comProcessModel);
    }
    
    @RequestMapping(value = "/comProcessObject", method = RequestMethod.GET)
    public ResponseEntity<String> detail(@RequestParam("comProcessId") String comProcessId) {
        Map<String, Object> comProcessModel = comProcessService.comProcessObject(comProcessId);

        return ResponseUtil.getResEntityForGetAndJson(comProcessModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        ComProcessDTO comProcessDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        ComProcess comProcessModel = comProcessDTO.toModel();

        return ResponseUtil.getResEntityForPPP(comProcessService.save(comProcessModel));
    }

    @RequestMapping(value = "/{comProcessId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("comProcessId") String comProcessId,
        @Validated
        ComProcessDTO comProcessDTO, BindingResult errors) {

        ComProcess comProcessModel = comProcessDTO.toModel();
        comProcessModel.setComProcessId(comProcessId);
        return ResponseUtil.getResEntityForPPP(comProcessService.update(comProcessModel));
    }
    
    @RequestMapping(value = "/comProcessUpdate", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        ComProcessDTO comProcessDTO, BindingResult errors) {

        ComProcess comProcessModel = comProcessDTO.toModel();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        comProcessModel.setStartTime(sdf.format(new Date(Long.valueOf(comProcessModel.getStartTime()))));
        comProcessModel.setEndTime(sdf.format(new Date(Long.valueOf(comProcessModel.getEndTime()))));
        System.out.println(comProcessModel.getStartTime());
        comProcessModel.setComProcessId(comProcessDTO.getComProcessId());
        return ResponseUtil.getResEntityForPPP(comProcessService.update(comProcessModel));
    }
    
    

    @RequestMapping(value = "/{comProcessId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("comProcessId") String comProcessId) {
        return ResponseUtil.getResEntityForDel(comProcessService.delete(comProcessId));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(comProcessService.batchDelete(primaryKeys));
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@RequestParam("comProcessId") String comProcessId) {
        return ResponseUtil.getResEntityForDel(comProcessService.delete(comProcessId));
    }
}
